﻿using AutoMapper;

using Microsoft.IdentityModel.Tokens;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ZFB.Common;
using ZFB.DTO;
using ZFB.IServices;
using ZFB.Repository.Permission;
using ZFB.Repository.Role;

namespace ZFB.Services
{
    /// <summary>
    /// 权限
    /// </summary>
    public class PermissionServices : IPermissionServices
    {
        private readonly IPermissionRepository permissionRepository;
        private readonly IRoleRepository _roleRepository;
        private readonly IMapper mapper;

        public PermissionServices(IRoleRepository roleRepository, IPermissionRepository permissionRepository, IMapper mapper)
        {
            _roleRepository = roleRepository;
            this.permissionRepository = permissionRepository;
            this.mapper = mapper;
        }
        public async Task<ApiResult> AddPermIdsAsync(long roleId, long[] permIds)
        {
           var role=await _roleRepository.GetModelAsync(e => e.Id == roleId);
            if (role == null) return new("角色不存在");
          var perm= await permissionRepository.GetListAsync(e => permIds.Contains(e.Id));
            foreach (var item in perm) 
            {
                role.T_Permissions.Add(item);
            }
          var res= await _roleRepository.AddAsync(role);
          return res <= 0 ? new ApiResult("添加失败！") : new ApiResult();

        }

        public async Task<ApiResult> GetAllAsync()
        {
            var data= await permissionRepository.GetListAsync();
            if (data.IsNullOrEmpty()) return new("获取失败!");
            var dto=mapper.Map<List<PermissionDTO>>(data);
            return new(dto);
        }

        public async Task<ApiResult> GetByIdAsync(long id)
        {
            var data = await permissionRepository.GetModelAsync(e => e.Id == id);
            if (data == null) return new("id不存在");
            var dto = mapper.Map<PermissionDTO>(data);
            return new(dto);
        }

        public async Task<ApiResult> GetByNameAsync(string name)
        {
            var data = await permissionRepository.GetModelAsync(e => e.Name == name);
            if (data == null) return new("name不存在");
            var dto = mapper.Map<PermissionDTO>(data);
            return new(dto);
        }

        public async Task<ApiResult> GetByRoleIdAsync(long roleId)
        {
          var role=  await _roleRepository.GetModelAsync(e => e.Id == roleId);
            if (role == null) return new("角色不存在");
            if (role.T_Permissions.Count <=0) return new("角色没有权限");
            var dto = mapper.Map<List<PermissionDTO>>(role.T_Permissions);
            return new(dto);
        }

        public async Task<ApiResult> UpdatePermIds(long roleId, long[] permIds)
        {
            var role = await _roleRepository.GetModelAsync(e => e.Id == roleId);
            if (role == null) return new("角色不存在");
            var perm = await permissionRepository.GetListAsync(e => permIds.Contains(e.Id));
            role.T_Permissions.Clear();
            foreach (var item in perm)
            {
                role.T_Permissions.Add(item);
            }
            var res = await _roleRepository.UpdateAsync(role);
            return res <= 0 ? new ApiResult("修改失败！") : new ApiResult();
        }
    }
}
